using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AdHocReports
{
    public partial class step3 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MasterPage.NextEnabled = true;
            MasterPage.BackEnabled = true;
            MasterPage.CancelEnabled = true;
            MasterPage.NextClicked += new EventHandler(mp_NextClicked);
            MasterPage.BackClicked += new EventHandler(mp_BackClicked);
            MasterPage.CancelClicked += new EventHandler(mp_CancelClicked);
            MasterPage.ContentTitle = "Create a Report: Select Columns";

            if (!IsPostBack && !IsCallback)
            {
                if (ReportDefinition.View.Length > 0)
                {
                    // Create a connection object
                    using (System.Data.OleDb.OleDbConnection oleConn = new System.Data.OleDb.OleDbConnection(ReportDefinition.ConnectionString))
                    {
                        // Create a data table
                        System.Data.DataTable columnTable = new System.Data.DataTable();
                        // Create a data adapter with column info
                        using (System.Data.OleDb.OleDbDataAdapter oleDA = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM \"" + ReportDefinition.View + "\" WHERE 0=1", oleConn))
                        {
                            // Fill the data table
                            oleDA.Fill(columnTable);
                        }

                        // Loop through the columns
                        for (int i = 0; i < columnTable.Columns.Count; i++)
                        {
                            switch (columnTable.Columns[i].DataType.ToString())
                            {
                                case "System.Boolean":
                                case "System.Char":
                                case "System.String":
                                case "System.DateTime":
                                case "System.Decimal":
                                case "System.Double":
                                case "System.Int16":
                                case "System.Int32":
                                case "System.Int64":
                                case "System.Single":
                                case "System.UInt16":
                                case "System.UInt32":
                                case "System.UInt64":
                                    // Create and add column info to select box
                                    System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem(columnTable.Columns[i].ColumnName, columnTable.Columns[i].ColumnName);
                                    lstCols.Items.Add(li);
                                    break;
                            }
                        }
                    }
                }

                lstSelect.Items.Clear();

                foreach (string selectedColumn in ReportDefinition.SelectColumns)
                {
                    lstSelect.Items.Add(new ListItem(selectedColumn, selectedColumn));
                }
            }
        }

        void mp_CancelClicked(object sender, EventArgs e)
        {
            Response.Redirect("~/");
        }

        void mp_BackClicked(object sender, EventArgs e)
        {
            // Update the selected columns
            UpdateSelectedColumns();

            // Go to the previous step
            Response.Redirect("step2.aspx");
        }

        void mp_NextClicked(object sender, EventArgs e)
        {
            // Update the selected columns
            UpdateSelectedColumns();

            if (ReportDefinition.SelectColumns.Count > 0)
            {
                // Go to the next step
                Response.Redirect("step4.aspx");
            }
        }

        private void UpdateSelectedColumns()
        {
            // Empty the list
            ReportDefinition.SelectColumns.Clear();

            for (int i = 0; i < lstSelect.Items.Count; i++)
            {
                ReportDefinition.SelectColumns.Add(lstSelect.Items[i].Value);
            }
        }

        protected void cmdMoveRight_Click(object sender, EventArgs e)
        {
            // Loop through all available columns
            for (int i = 0; i < lstCols.Items.Count; i++)
            {
                // If the current item is selected
                if (lstCols.Items[i].Selected == true)
                {
                    // Add it to the "Selected Columns" listbox
                    lstSelect.Items.Add(lstCols.Items[i]);
                    // Deselect the item
                    lstCols.Items[i].Selected = false;
                }
            }

            // Update the selected columns
            UpdateSelectedColumns();
        }
        protected void cmdMoveLeft_Click(object sender, EventArgs e)
        {
            // Loop backwards through the "Selected Columns" list
            for (int i = (lstSelect.Items.Count - 1); i >= 0; i--)
            {
                // If the current item is selected
                if (lstSelect.Items[i].Selected == true)
                {
                    // Take it off the list
                    lstSelect.Items.Remove(lstSelect.Items[i]);
                }
            }

            // Update the selected columns
            UpdateSelectedColumns();
        }
        protected void cmdMoveUp_Click(object sender, EventArgs e)
        {
            // Loop through the list items (ignoring the top element)
            for (int i = 1; i < lstSelect.Items.Count; i++)
            {
                // If the current item is selected
                if (lstSelect.Items[i].Selected == true)
                {
                    // Create a new list item, matching the selected list item
                    System.Web.UI.WebControls.ListItem liBottom = lstSelect.Items[i];
                    // Remove the selected item
                    lstSelect.Items.Remove(lstSelect.Items[i]);
                    // Insert the same element one index up
                    lstSelect.Items.Insert(i - 1, liBottom);
                    // Stop after processing the first "swap"
                    break;
                }
            }

            // Update the selected columns
            UpdateSelectedColumns();
        }

        protected void cmdMoveDown_Click(object sender, EventArgs e)
        {
            // Loop backwards through the loop, ignoring the bottom element
            for (int i = (lstSelect.Items.Count - 2); i >= 0; i--)
            {
                // If the current item is selected
                if (lstSelect.Items[i].Selected == true)
                {
                    // Create a new list item, matching the selected item
                    System.Web.UI.WebControls.ListItem liTop = lstSelect.Items[i];
                    // Remove the current item
                    lstSelect.Items.Remove(lstSelect.Items[i]);
                    // Add the item one index down
                    lstSelect.Items.Insert(i + 1, liTop);
                    // Stop processing the first "swap"
                    break;
                }
            }

            // Update the selected columns
            UpdateSelectedColumns();
        }
    }
}